package com.mirror.utils.view;

import com.mirror.llk.R;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class CustomRatingBar extends LinearLayout implements Runnable {

	private static String TAG = "CustomRatingBar";

	private Context context;

	private float score;

	private int weight;

	private static final float TOTAL_SCORE = 5f;

	private boolean start = false, stop = false;

	private View view1, view2;

	private boolean editAble = false, showScore = true;

	private TextView scoreView;

	public View getView1() {
		return view1;
	}

	private LinearLayout.LayoutParams params1, params2;

	public CustomRatingBar(Context context) {
		super(context);
		this.context = context;
		init();
	}

	@SuppressLint("Recycle")
	public CustomRatingBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		if (attrs != null) {
			TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomRatingBar);
			editAble = a.getBoolean(R.styleable.CustomRatingBar_editAble, false);
			showScore = a.getBoolean(R.styleable.CustomRatingBar_showScore, true);
		}
		this.context = context;
		init();
	}

	@SuppressLint("NewApi")
	@SuppressWarnings("deprecation")
	public void init() {
		this.setOrientation(LinearLayout.HORIZONTAL);

		// 设置rating_bar
		RelativeLayout relativeLayout = new RelativeLayout(context);
		relativeLayout.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.rating_bar_star));
		LinearLayout.LayoutParams linearLayoutParams = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.FILL_PARENT);
		linearLayoutParams.weight = 1;
		relativeLayout.setLayoutParams(linearLayoutParams);

		// 设置rating_bar的底层
		LinearLayout linearLayout = new LinearLayout(context);
		RelativeLayout.LayoutParams relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
		        RelativeLayout.LayoutParams.FILL_PARENT);
		linearLayout.setLayoutParams(relativeLayoutParams);
		linearLayout.setOrientation(LinearLayout.HORIZONTAL);
		params1 = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.FILL_PARENT);
		params1.weight = 0;
		view1 = new View(context);
		view1.setLayoutParams(params1);
		view1.setBackgroundColor(context.getResources().getColor(R.color.alpha_00));
		linearLayout.addView(view1);
		view2 = new View(context);
		params2 = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.FILL_PARENT);
		params2.weight = 100;
		view2.setLayoutParams(params2);
		view2.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.rating_bar_dark_bg));
		linearLayout.addView(view2);
		relativeLayout.addView(linearLayout);

		// 设置rating_bar的表层
		View ratingBar = new View(context);
		ratingBar.setLayoutParams(relativeLayoutParams);
		ratingBar.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.rating_bar));
		relativeLayout.addView(ratingBar);
		relativeLayout.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (editAble) {
					float score = 5 * event.getX() / v.getWidth();
					if (score < 0.25) {
						score = 0f;
					} else if (score < 0.75) {
						score = 0.5f;
					} else if (score < 1.25) {
						score = 1f;
					} else if (score < 1.75) {
						score = 1.5f;
					} else if (score < 2.25) {
						score = 2f;
					} else if (score < 2.75) {
						score = 2.5f;
					} else if (score < 3.25) {
						score = 3f;
					} else if (score < 3.75) {
						score = 3.5f;
					} else if (score < 4.25) {
						score = 4f;
					} else if (score < 4.75) {
						score = 4.5f;
					} else {
						score = 5f;
					}
					params1.weight = 100 * score / 5;
					view1.setLayoutParams(params1);
					params2.weight = 100 - params1.weight;
					view2.setLayoutParams(params2);
					setScoreValue(score);
					Log.i(TAG, "score:" + score);
					return true;
				} else {
					return false;
				}
			}
		});
		addView(relativeLayout);

		// 设置比例

		scoreView = new TextView(context);
		scoreView.setLayoutParams(new LinearLayout.LayoutParams(50, LinearLayout.LayoutParams.FILL_PARENT));
		scoreView.setGravity(Gravity.CENTER);
		scoreView.setBackgroundColor(Color.WHITE);
		if (showScore) {
			scoreView.setVisibility(VISIBLE);
		} else {
			scoreView.setVisibility(GONE);
		}
		addView(scoreView);
	}

	@Override
	public void run() {
		if (editAble) {
			return;
		}
		if (stop) {
			return;
		}
		if (params1.weight >= this.weight) {
			stop = true;
			return;
		}
		params1.weight += 1;
		view1.setLayoutParams(params1);
		params2.weight = 100 - params1.weight;
		view2.setLayoutParams(params2);
		postDelayed(this, 5);
	}

	public float getScore() {
		return score;
	}

	public void setScoreValue(float score) {
		this.score = score;
		this.scoreView.setText(String.valueOf(score));
		postDelayed(this, 5);
	}

	public void setScore(float score) {
		if (this.start) {
			return;
		}
		if (score >= 0 && score <= CustomRatingBar.TOTAL_SCORE) {
			this.score = score;
			this.scoreView.setText(String.valueOf(score));
			this.weight = (int) (score / CustomRatingBar.TOTAL_SCORE * 100);
			Log.i(TAG, "this.weight:" + this.weight);
			if (this.weight % 20 == 2) {// 2->4
				this.weight += 2;
			} else if (this.weight % 20 == 4) {// 4->5
				this.weight += 1;
			} else if (this.weight % 20 == 16) {// 16->15
				this.weight -= 1;
			} else if (this.weight % 20 == 18) {// 18->16
				this.weight -= 2;
			}
			Log.i(TAG, "this.weight%20:" + this.weight);
			this.start = true;
			postDelayed(this, 1000);
		} else {
			this.stop = true;
		}
	}

}
